// pages/index/search/search.js
const common = require('../../utils/login.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    closeImg: 'https://gitee.com/Anjina/vote-pic/raw/7b66d388a031fdd545ce9a4ffc6a31e74de6710c/assets/Combined%20Shape@2x.png',
    searchVal: '',
    isInput: false,
    size: 20,
    page: 0,
    list: [],
    videoList: [],
    textList: [],
    picList: [],
    variable: {
      userBtn: true
    },
    token: ''
  },
  searchFor: function(e) {
    var that = this
    var value = e.detail.value
    if(value != ""){
      that.setData({
        isInput: true
      })
    }
    if(value === ''){
      that.setData({
        isInput: false
      })
    }
    that.setData({
      searchVal: value
    })
  },
  finishSearch: function (e) {
    var value = e.detail.value
    if(value!= "") {
      this.searchResult(value)
    }
    this.setData({
      searchVal: value
    })
  },
  clear: function() {
    this.setData({
      searchVal: '',
      isInput: false
    })
  },
  toDetails: function (e) {
    let id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    switch (type) {
      case 'pic':
        wx.navigateTo({
          url: '../pic-details/pic-details?id=' + id,
        })
        break
      case 'video':
        wx.navigateTo({
          url: '../video-details/video-details?id=' + id,
        })
        break
      case 'article':
        wx.navigateTo({
          url: '../article-details/article-details?id=' + id,
        })
      default:
        console.log('toDetailsError')
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.isUserInfo()
    this.searchResult(options.searchVal);
    if (options.searchVal!= ''){
      this.setData({
        searchVal: options.searchVal,
        isInput: true
      })
      this.searchResult(options.searchVal)
    }
    else{
      this.setData({
        searchVal: ''
      })
    }
  },
  // 搜索结果
  searchResult(searchVal) {
      let that = this;
      wx.request({
        url: 'https://m.feawin.com/vote/list/query?activityId=' + 1 + '&keyword=' + searchVal,
        data: {
          size: that.data.size, page: that.data.page
        },
        method: 'get',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          // 'custom': JSON.stringify(header),
          'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
        },
        success: function (res) {
          console.log(res.data.list);
          let allList = res.data.list;
          let videoList = [], textList = [], picList = [];
          for (let i = 0; i < allList.length; i++) {
            if (allList[i].contentType === 'video') {
              videoList.push(allList[i])
            } else if (allList[i].contentType === 'text') {
              textList.push(allList[i])
            } else {
              picList.push(allList[i])
            }
          }
          that.setData({
            videoList: videoList,
            textList: textList,
            picList: picList
          })
        },
        fail: function (res) { },
        complete: function (res) { },
      })
  },
  // 判断是否有用户信息
  isUserInfo() {
    let userInfo = wx.getStorageSync('userInfo');
    let userBtn = 'variable.userBtn';
    if (userInfo) {
      this.setData({
        [userBtn]: false
      })
    }
  },
  // 登录
  bindGetUserInfo(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      common.wxLogin().then(data => {
        console.log(data);
        let userBtn = 'variable.userBtn';
        that.setData({
          token: data.token,
          [userBtn]: false
        })
        wx.setStorageSync('userInfo', data.token); // 保存用户信息
        that.Voting(id);  // 登录之后调用投票
      });
    }
  },
  // 点击投票
  toupiao(e) {
    console.log(e);
    let id = e.currentTarget.dataset.id;
    this.Voting(id);
  },
  // 投票请求封装
  Voting: function (id) {
    console.log(id);
    let that = this;
    wx.request({
      url: 'https://m.feawin.com/vote/add/' + id,
      data: '',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '投票成功!',
          icon: 'success',
          duration: 2000
        })
        that.searchResult(that.data.searchVal)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 已投票
  Voted() {
    console.log('已投票');
  },

})